#include "task_period.h"
//#include "hal_sig.h"
#include "watchdog.h"

PeriodEvt    period_event;
IDLE_QUEUE_MSG period_queue[5];

extern void sig_timeout(void);
static volatile unsigned char led_state = 0;
static RAW_U16 period_fsm_initial(PeriodEvt *me, STATE_EVENT *e);
static RAW_U16 period_fsm_circle(PeriodEvt *me, STATE_EVENT *e);

static RAW_U16 period_fsm_initial(PeriodEvt *me, STATE_EVENT *e)
{
    return STM_TRAN(period_fsm_circle);
}

static RAW_U16 period_fsm_circle(PeriodEvt *me, STATE_EVENT *e)
{
    RAW_U16 status = STM_RET_IGNORED;
    switch(e->sig)
    {
        case STM_ENTRY_SIG:
            event_tick_arm(&(me->evt), 20);    
            status = STM_RET_HANDLED;
        break;
        case STM_TIMEOUT_SIG:
            event_tick_arm(&(me->evt), 35);    
            if(0 == led_state)
						{
							led_state = 1;
							led_on();
						}
						else
						{
							led_state = 0;
							led_off();
						}

						WDOG_DRIVE.feed();
						sig_timeout();
            status = STM_RET_HANDLED;
        break;
        default:
            status = STM_RET_IGNORED;
        break;
    }
    return status;
}

void period_task_init(void)
{
    FSM_CONSTRUCTOR(&period_event.evt.super, (stm_state_handler)period_fsm_initial);
    fsm_init(&period_event.evt.super, 0);
}
